package com.zmn.plat.admin.controller.orderconfig.cancel.reason;

import java.util.Collections;
import java.util.List;
import java.util.Optional;

import javax.annotation.Resource;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.zmn.common.constant.GlobalConsts;
import com.zmn.plat.manager.dictionary.ProductDict;
import com.zmn.plat.model.entity.orderconfig.cancel.OrderCancelCategory;
import com.zmn.plat.model.entity.orderconfig.cancel.OrderCancelCategoryQuery;
import com.zmn.plat.model.entity.orderconfig.cancel.OrderCancelReason;
import com.zmn.plat.model.entity.orderconfig.cancel.OrderCancelReasonQuery;
import com.zmn.plat.model.vo.orderconfig.cancel.OrderCancelReasonVO;
import com.zmn.plat.services.interfaces.orderconfig.cancel.OrderCancelCategoryService;

/**
 * @author tanbiao
 * @description：订单配置取消缘由
 * @since 2019/10/21 16:49
 * @modifier
 **/
@Controller
@RequestMapping("/orderconfig/cancelReason/")
public class OrderCancelReasonListController extends OrderCancelReasonBaseController {
    private Logger logger = LoggerFactory.getLogger(OrderCancelReasonListController.class);

    @Resource
    private OrderCancelCategoryService orderCancelCategoryService;

    /**
     * 描述: 取消缘由列表
     *
     * @param query
     *            查询实体
     * @return ModelAndView
     * @author tanbiao
     * @since 15:38 2019/10/22
     */
    @SuppressWarnings("Duplicates")
    @RequestMapping(value = "list")
    @RequiresPermissions("/orderconfig/cancelReason/list.action")
    public ModelAndView list(@ModelAttribute OrderCancelReasonQuery query) {
        ModelAndView mav = new ModelAndView("orderconfig/cancel/reason/list");
        int count = orderCancelReasonService.countByQuery(query);
        query.setTotalCount(count);
        List<OrderCancelReasonVO> cancelCategories = orderCancelReasonService.listPageByQuery(query);
        // 取消分类
        List<OrderCancelCategory> list = orderCancelCategoryService.listByQuery(new OrderCancelCategoryQuery());
        mav.addObject("items", cancelCategories);
        mav.addObject("vo", query);
        mav.addObject("bizTypeList", com.zmn.plat.common.dictionary.ProductDict.listBizType());
        mav.addObject("cancelTypeList", ProductDict.getCancelTypeList());
        mav.addObject("cancelNodeList", ProductDict.getCancelNodeList());
        mav.addObject("cancelCategorys", list);
        return mav;
    }

    /**
     * 功能描述 ：详情页面
     *
     * @param id
     * @return org.springframework.web.servlet.ModelAndView
     * @author tanbiao
     * @since 2019/10/22
     * @modifier
     */
    @RequestMapping(value = "detail")
    @RequiresPermissions("/orderconfig/cancelReason/detail.action")
    public ModelAndView detail(@RequestParam(value = "id", defaultValue = "0") Integer id) {
        ModelAndView mav = new ModelAndView("orderconfig/cancel/reason/detail");
        OrderCancelReason orderCancelReason = new OrderCancelReason();
        if (id == 0) {
            orderCancelReason = new OrderCancelReason();
        } else {
            orderCancelReason = Optional.ofNullable(orderCancelReasonService.findByKey(id)).orElse(orderCancelReason);
        }
        if (orderCancelReason.getSort() == null) {
            Integer sort = orderCancelReasonService.findMaxSort();
            sort = sort == null ? 1 : sort + 1;
            orderCancelReason.setSort(sort);
        }
        // 取消分类,只显示启用状态的
        OrderCancelCategoryQuery query = new OrderCancelCategoryQuery();
        query.setStatus(GlobalConsts.YES);
        List<OrderCancelCategory> list = orderCancelCategoryService.listByQuery(query);
        // 如果是修改的情况下，那么也要让禁用的分类出现
        if (orderCancelReason.getId() != null) {
            OrderCancelCategory cancelCategory = orderCancelCategoryService.findByKey(orderCancelReason.getCategId());
            if (cancelCategory != null && cancelCategory.getStatus().equals(GlobalConsts.NO)) {
                list.add(cancelCategory);
                Collections.sort(list);
            }
        }

        mav.addObject("reason", orderCancelReason);
        mav.addObject("cancelCategorys", list);
        mav.addObject("cancelNodeList", ProductDict.getCancelNodeList());
        mav.addObject("cancelTypeList", ProductDict.getCancelTypeList());
        mav.addObject("bizTypeList", com.zmn.plat.common.dictionary.ProductDict.listBizType());
        return mav;
    }
}
